﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.Security;
using System.Security;
using System.Web;
using ActionModel.Utility;
using System.Diagnostics;

namespace ActionModel.WebModel
{
	/// <summary>
	/// 基本页面模型
	/// </summary>
	public class PageBase : Page
	{
		protected Stopwatch sw = new Stopwatch();
		Dictionary<string, Action> _ActionDict = new Dictionary<string, Action>();
		/// <summary>
		/// Action 调用方法目录
		/// </summary>
		protected virtual Dictionary<string, Action> ActionDict
		{
			get { return _ActionDict; }
			set { _ActionDict = value; }
		}
		protected string RequestAction { get { return Request.QueryString["action"]; } }

		/// <summary>
		/// 页面已执行时间
		/// </summary>
		protected TimeSpan PageSpendTime
		{
			get { return sw.Elapsed; }
		}

		protected PageBase()
		{
			sw.Start();
		}
		
		/// <summary>
		/// 在页加载阶段结束时引发 System.Web.UI.Page.LoadComplete 事件。
		/// </summary>
		/// <param name="e">包含事件数据的 System.EventArgs。</param>
		protected override void OnLoadComplete(EventArgs e)
		{
			if (Request.HttpMethod == "GET")
			{
				OnHttpGET();
			}
			else if (Request.HttpMethod == "POST")
			{
				OnHttpPOST();
			}
			base.OnLoadComplete(e);
		}

		/// <summary>
		/// 当HttpMethod为“GET”时的处理操作
		/// </summary>
		protected virtual void OnHttpGET() { }
		/// <summary>
		/// 当HttpMethod为“POST”时的处理操作
		/// </summary>
		protected virtual void OnHttpPOST()
		{
			if (!string.IsNullOrEmpty(RequestAction))
			{
				if (ActionDict.ContainsKey(RequestAction))
				{
					Action action = ActionDict[RequestAction];
					action();
				}

				Response.End();
			}
		}

		void targ() { }
	}
}
